package com.ruoyi.common.utils;

import java.util.HashMap;
import java.util.Map;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV2Request;
import com.baidubce.services.sms.model.SendMessageV2Response;
import com.baidubce.services.sms.model.SendMessageV3Request;
import com.baidubce.services.sms.model.SendMessageV3Response;

/**
 * 百度智能云发送短信服务
 */
public class BaiDuSms {
    private static String endPoint = "http://smsv3.bj.baidubce.com";// SMS服务域名，可根据环境选择具体域名
    private static String accessKeyId = "e38bfdb8e14b461f80908a60efcd1e0b";// 发送账号安全认证的Access Key ID
    private static String secretAccessKy = "a48110a938e24f748302686d15073d79"; // 发送账号安全认证的Secret Access Key

    public static void main(String[] args) throws Exception {
        sendSms("18311489999", "zyt");
        //sendSms("18311489999", "zyt", "增加", "50");
        //sendSmsCode("18311489999", "4385");
    }
    /**
     * 发送短信
     * @param telephone
     * @param name
     * @return
     * @throws Exception
     */
    public static String sendSms(String telephone,String name) throws Exception {
        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy));
        config.setEndpoint(endPoint);
        SmsClient client = new SmsClient(config);
        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(telephone);
        request.setSignatureId("sms-sign-LapcJs69169");
        request.setTemplate("sms-tmpl-jCybLl61508");
        Map<String, String> contentVar = new HashMap<>();
        contentVar.put("name", name);
        request.setContentVar(contentVar);
        SendMessageV3Response response = client.sendMessage(request);
        if (response != null && response.isSuccess()) {
            System.out.println(response.toString());
        } else {
            System.out.println(response.toString());
        }
        return response.getCode();
    }

    /**
     * 发送短信
     * @param telephone
     * @param name
     * @param method
     * @param number
     * @return
     * @throws Exception
     */
    public static String sendSms(String telephone, String name, String method, String number) throws Exception {
        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy));
        config.setEndpoint(endPoint);
        SmsClient client = new SmsClient(config);
        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(telephone);
        request.setSignatureId("sms-sign-LapcJs69169");
        request.setTemplate("sms-tmpl-nOCDhp84614");
        Map<String, String> contentVar = new HashMap<>();
        contentVar.put("name", name);
        contentVar.put("method", method);
        contentVar.put("number", number);
        request.setContentVar(contentVar);
        SendMessageV3Response response = client.sendMessage(request);
        if (response != null && response.isSuccess()) {
            System.out.println(response.toString());
        } else {
            System.out.println(response.toString());
        }
        return response.getCode();
    }

    /**
     * 发送验证码
     * @param telephone
     * @param code
     * @return
     * @throws Exception
     */
    public static String sendSmsCode(String telephone,String code) throws Exception{
        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKy));
        config.setEndpoint(endPoint);
        SmsClient client = new SmsClient(config);
        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(telephone);
        request.setSignatureId("sms-sign-LapcJs69169");
        request.setTemplate("sms-tmpl-RXWtLI41533");
        Map<String, String> contentVar = new HashMap<>();
        contentVar.put("code", code);
        request.setContentVar(contentVar);
        SendMessageV3Response response = client.sendMessage(request);
        if (response != null && response.isSuccess()) {
            System.out.println(response.toString());
        } else {
            System.out.println(response.toString());
        }
        return response.getCode();
    }


}

